﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Frame.Lib.Core.Dict;
using Frame.Lib.Utilities;

public partial class Common_Dict_Edit : PageEdit
{
    DictBO dict = new DictBO();  

    private int ItemID
    {
        get
        {
            return int.Parse(Request["ItemID"]);
        }
    }   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            if (PKID2 != null && PKID2 != "")
            {
                DispInfo();
            }
            else
            {
                UpperID.Value = ItemID.ToString();
                Code.Text = dict.GetMaxCode(ItemID).ToString();
            }
            WebUtil.SetSaveBtn(Request["ID"], btnSave);
        }
    }

    private void DispInfo()
    {
        DictInfo info = dict.GetInfo(PKID2);
        EntityUtil.SetControl(pnlMain, info);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strName = Name.Text.Trim();
        bool flag = dict.Exist(strName, PKID2, int.Parse(UpperID.Value)) || dict.ExistCode(int.Parse(Code.Text),PKID2,int.Parse(UpperID.Value));
        if (flag)
        {
            JSUtil.Alert("该名称或代码已存在");
        }
        else
        {
            DictInfo info = dict.GetInfo(PKID2);
            EntityUtil.SetEntity(pnlMain, info);

            dict.Save(info);
            if (PKID2 == null)
            {
                JSUtil.RefreshView(this);
                ClearInfo();
            }
            else
            {
                JSUtil.RefreshView();
            }
        }
    }
    
    private void ClearInfo()
    {
        PKID2 = null;
        Name.Text = "";
        Code.Text = dict.GetMaxCode(ItemID).ToString(); ;
        Remark.Text = "";
    }
}
